package com.moxiu.browser.util;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Random;

import com.moxiu.launcher.R;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Build;
import android.telephony.TelephonyManager;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.WindowManager;

public class Common {
	
	public static String SOURCE = "source";

	/**
	 * 获取 应用版本号
	 */
	public static String getVersionCode(Context context) {
		PackageManager manager = context.getApplicationContext().getPackageManager();
		int versionCode = 0;
		try {
			PackageInfo info = manager.getPackageInfo(context.getApplicationContext().getPackageName(), 0);
			versionCode = info.versionCode;
		} catch (NameNotFoundException e) {
			e.printStackTrace();
		} catch (OutOfMemoryError error) {
		}

		return versionCode + "";
	}

	/**
	 * 获取 应用版本名
	 */
	public static String getVersionName(Context context) {
		PackageManager manager = context.getApplicationContext().getPackageManager();
		String versionName = "";
		try {
			PackageInfo info = manager.getPackageInfo(context.getApplicationContext().getPackageName(), 0);
			versionName = info.versionName;
		} catch (NameNotFoundException e) {
			e.printStackTrace();
		} catch (OutOfMemoryError error) {
		}

		return versionName;
	}

	/**
	 * 获取当前屏幕宽
	 * 
	 * @param c
	 * @return
	 */
	public static int getDisplayWidth(Context c) {
		DisplayMetrics display = c.getResources().getDisplayMetrics();
		return display.widthPixels;
	}

	/**
	 * 获取当前屏幕高
	 * 
	 * @param c
	 * @return
	 */
	public static int getDisplayHeight(Context c) {
		DisplayMetrics display = c.getResources().getDisplayMetrics();
		return display.heightPixels;
	}

	// 像素转dip
	public static float PixelsToDip(Context context, int Pixels) {
		final float SCALE = context.getResources().getDisplayMetrics().density;
		int dips = (int) (Pixels / SCALE + 0.5f);
		if (SCALE > 2.5) {
			dips = dips + 100;
		}
		return dips;
	}

	// dp转像素
	public static float DipToPixels(Context context, int dp) {
		final float density = context.getResources().getDisplayMetrics().density;
		return dp * density;
	}

	public static String getMobileInformation(Context mContext) {
		int screenWidth = 0;
		int screenHeight = 0;
		String ver = null;
		String model = null;
		String release = null;
		String imei = null;
		String dpi = null;
		String moblieInformation = null;
		int versionCode = 0;

//		SharedPreferences settings = null;
		String curhome = null;

//		try {
//			settings = mContext.getSharedPreferences("moxiu_theme_config", Context.MODE_WORLD_READABLE);
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//		curhome = settings.getString("current_home_package_name", "com.android.adwlauncher");
		screenWidth = getDisplayWidth(mContext);
		screenHeight = getDisplayHeight(mContext);
		dpi = String.valueOf(screenWidth) + "X" + String.valueOf(screenHeight);
		TelephonyManager tm = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
		imei = tm.getDeviceId();
		if (imei == null) {
			int aa = (int) Math.pow(10, 14);
			Random random = new Random();
			imei = random.nextInt(aa) + aa + "";
		}

		model = Build.MODEL;
		// model = setReplace(model);
		release = Build.VERSION.RELEASE;
		release = setReplace(release);
		try {
			PackageManager manager = mContext.getPackageManager();
			PackageInfo info = manager.getPackageInfo(mContext.getPackageName(), 0);
			ver = info.versionName;
			versionCode = info.versionCode;
		} catch (Exception e) {
			e.printStackTrace();
		}
//		String child = mContext.getResources().getString(R.string.t_market_theme_manager_child); //
		// 渠道
		String mmodel = null;

		// http://10.0.0.24:8090/obtain?model=&dip=&ver=&imsi=&imei=&release=&curhome=&chlId=&open_count=&lon=&lat=
		try {
			mmodel = URLEncoder.encode(model, "utf-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		// http://10.0.0.24:8090/obtain?model=&dip=&ver=&imsi=&imei=&release=&curhome=&chlId=&open_count=&lon=&lat=
//		moblieInformation = "&model=" + mmodel + "&dpi=" + dpi + "&ver=" + ver + "&vcode=" + versionCode + "&imei="
//				+ imei + "&release=" + release + "&child=" + child + "&curhome=" + curhome;

		moblieInformation = "&model=" + mmodel + "&dpi=" + dpi + "&ver=" + ver + "&vcode=" + versionCode + "&imei="
				+ imei + "&release=" + release +  "&child=" + mContext.getResources().getString(R.string.t_market_theme_manager_child);
		
		return moblieInformation;
	}

	public static String setReplace(String str) {
		String str_replace = str.replace(" ", "");
		return str_replace;
	}
}
